﻿using AutoMapper;
using Comm100.Application.Services;
using Comm100.Framework.Auditing;
using Comm100.Framework.Authorization;
using Comm100.Public.Audit;
using Bot.Application.Dto;
using Bot.Application.Services;
using Bot.Domain.Bo;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Text;
using Bot.Domain;
using Comm100.Framework;
using Bot.Domain.Specifications;

namespace Bot.Application.LearningQuestions.Service
{
    public class AgentAssistLearningQuestionAppService : BaseAppService, IAgentAssistLearningQuestionAppService
    {
        private IAgentAssistLearningQuestionDomainService _domainService;

        public AgentAssistLearningQuestionAppService(IAgentAssistLearningQuestionDomainService domainService) : base()
        {
            this._domainService = domainService;
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        [Audit(BotEntity.LEARNINGQUESTION, AuditAction.DESTROY)]
        public void Delete(Guid id)
        {
            _domainService.Delete(id);
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        public AgentAssistLearningQuestionDto Create(AgentAssistLearningQuestionCreateDto dto)
        {
            //LearningQuestion learningQuestion = _domainService.Create(Mapper.Map<LearningQuestion>(dto));
            //return Mapper.Map<LearningQuestionDto>(learningQuestion);
            var obj = Mapper.Map<AgentAssistLearningQuestion>(dto);
            obj.Id = Guid.NewGuid();
            obj.CreatedTime = DateTime.UtcNow;
            AgentAssistLearningQuestion temp = _domainService.Create(obj);
            return Mapper.Map<AgentAssistLearningQuestionDto>(temp);
        }

        public AgentAssistLearningQuestionDto Get(Guid id)
        {
            AgentAssistLearningQuestion learningQuestion = _domainService.Get(id);
            return Mapper.Map<AgentAssistLearningQuestionDto>(learningQuestion);
        }

        public PagedListDto<AgentAssistLearningQuestionDto> GetList(LearningQuestionQueryDto dto, Paging paging)
        {
            AgentAssistLearningQuestionFilterSpecification spec = new AgentAssistLearningQuestionFilterSpecification(dto.TimeFrom, dto.TimeTo);
           
            if (string.IsNullOrEmpty(dto.Keyword) && dto.MinScore > 0)
            {
                spec = new AgentAssistLearningQuestionFilterSpecification(dto.TimeFrom, dto.TimeTo, dto.MinScore);
            }
            else if (!string.IsNullOrEmpty(dto.Keyword) && dto.MinScore <= 0)
            {
                spec = new AgentAssistLearningQuestionFilterSpecification(dto.TimeFrom, dto.TimeTo, dto.Keyword);
            }
            else if (!string.IsNullOrEmpty(dto.Keyword) && dto.MinScore > 0)
            {
                spec = new AgentAssistLearningQuestionFilterSpecification(dto.TimeFrom, dto.TimeTo, dto.Keyword, dto.MinScore);
            }
            int count = _domainService.Count(spec);
            spec.ApplyPaging(paging);
            IEnumerable<AgentAssistLearningQuestion> list = _domainService.List(spec);
            
            return new PagedListDto<AgentAssistLearningQuestionDto>(count, Mapper.Map<IEnumerable<AgentAssistLearningQuestionDto>>(list));
        }
    }
}
